// pages/attendance/monthReport/workHourDetail/workHourDetail.js

const app = getApp()
const networkUtil=app.globalData.networkUtil
const util=app.globalData.util

const logStrPrefix = "attendance.monthReport.workHourDetail"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        //工时类型(work:正常上班; overtime:加班;)
        workHourType: null,
        yearmonth: null,
        monthReport: null,

        //考勤日期列表
        dayList: [],
        //正常上班时数列表
        workdayList: [],
        //加班时数列表
        overtimedayList: []



    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this

        that.setData({
            yearmonth: options.yearmonth,
            workHourType: options.workHourType
        })

        /**
         * 1、获取用户月考勤信息
         */
        that.getMonthReport().then((res) => {
            if (res == app.globalData.SUCCESS_STR) {
                /**
                 * 2、从monthReport中获取正常上班时数、加班时数列表。
                 */
                that.getWorkHourList_fromMonthReport()
            }
        })
    },

    /**
     * 获取用户月考勤信息。
     */
    getMonthReport: function() {
        let that = this
        let url = app.globalData.baseUrl + "/attendance/getMonthReport"
        let requestData = {
            yearmonth: that.data.yearmonth.split("-").join("")
        }

        return networkUtil.request_get(url, requestData).then((res) => {
            //若已获取到monthReport
            that.setData({
                monthReport: res.data.monthReport,
                dayList: res.data.month
            })
            return app.globalData.SUCCESS_STR
        }).catch((res) => {
            //若未获取到monthReport
            console.error(logStrPrefix + ".getMonthReport() 获取月考勤信息,出错!")
            util.showToast("获取月考勤信息,出错!")
            return app.globalData.ERROR_STR
        })
    },

    /**
     * 从monthReport中获取正常上班时数、加班时数列表。
     */
    getWorkHourList_fromMonthReport: function() {
        let that = this
        let monthReport = that.data.monthReport

        //将正常上班时数存入workdayList中
        let workdayList = []
        for (let i = 1; i <= 31; i++) {
            workdayList.push(monthReport["workday" + util.getNumStr_withLength(i, 2)])
        }

        //将加班时数存入overtimedayList中
        let overtimedayList = []
        for (let i = 1; i <= 31; i++) {
            overtimedayList.push(monthReport["overtimeday" + util.getNumStr_withLength(i, 2)])
        }

        that.setData({
            workdayList: workdayList,
            overtimedayList: overtimedayList
        })
    },





    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})